<?php
/* 
 * controller for pets view
 * Ross Mathis
 */

class Pets extends Controller{

    function index()
    {
           
           $petlist = $this->pet->view_pets();

           $data = array();
           $data['pets']= $petlist;
					 $session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
           $data['title'] = "L & R Pets";
           
           $this->load->view('pets/index', $data);
          
    }
    function pets_detail($wherestring){

            $petslist = $this->pet->retrieve_details($wherestring);

            $data = array();
            $data["pets"] = $petslist[0];
$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
            $data['title'] = "L & R Pets";
            
           
           $this->load->view('pets/detail', $data);
           
            
            }
    function sort($by){
            if ($by == 'bypet')
            $petslist = $this->pet->orderby_pets();

            else if ($by == "byspecies")
            $petslist = $this->pet->orderby_species();
            
            else if ($by == "bybreed")
            $petslist = $this->pet->orderby_breed();

            $data = array();

            $data["pets"] = $petslist;
$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
            $data['title'] = "L & R Pets";
            
            $this->load->view('pets/index', $data);
            

            }
    function add($id){

            if (isset($_POST['pet_name']))
            {
            $this->form_validation->set_rules('pet_name', 'Pet Name', 'required|trim|xss_clean|alpha');
            $this->form_validation->set_rules('species', 'Species', 'required|trim|xss_clean|alpha');
            $this->form_validation->set_rules('breed', 'Breed', 'required|trim|xss_clean');
            $this->form_validation->set_rules('weight', 'Weight', 'numeric');
            $this->form_validation->set_rules('coloring', 'Coloring', 'trim|xss_clean');
            $this->form_validation->set_rules('knownconditions', 'Known Conditions', 'trim|xss_clean');
            $this->form_validation->set_rules('notes', 'Notes', 'trim|xss_clean');
         

                if ($this->form_validation->run() == FALSE)
                {
$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
                       $data['title'] = "L & R Pets";
                      
											 $data['id'] = $id;
                       $this->load->view('pets/addpet', $data);
                       
                       
                }
                else
                {

                $pet_name = $_POST['pet_name'];
                $species = $_POST['species'];
                $breed = $_POST['breed'];
                $weight = $_POST['weight'];
                $coloring = $_POST['coloring'];
                $knownconditions = $_POST['knownconditions'];
                $notes = $_POST['notes'];
               

                $this->pet->insert_pets($pet_name,$species,$breed,$weight,$coloring,$knownconditions, $notes,$id);
								$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
                 $data['title'] = "L & R Pets";
                
                   $this->load->view('pets/success', $data);
                   
               
		}
		}
		else{
            $data['id'] = $id;
						$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
                    $data['title'] = "L & R Pets";
                   
                   $this->load->view('pets/addpet', $data);
                  

		}
		}

                //----------------------------------------------------------
		function update($wherestring){

		if (isset($_POST['pet_name']) && isset($_POST['species']))
					{
                        $this->form_validation->set_rules('pet_name', 'Pet Name', 'required|trim|xss_clean|alpha');
                        $this->form_validation->set_rules('species', 'Species', 'required|trim|xss_clean|alpha');
                        $this->form_validation->set_rules('breed', 'Breed', 'required|trim|xss_clean');
                        $this->form_validation->set_rules('weight', 'Weight', 'numeric');
                        $this->form_validation->set_rules('coloring', 'Coloring', 'trim|xss_clean');
                        $this->form_validation->set_rules('knownconditions', 'Known Conditions', 'trim|xss_clean');
                        $this->form_validation->set_rules('notes', 'Notes', 'trim|xss_clean');

                        if ($this->form_validation->run() == FALSE)
                        {
                                $petslist = $this->pet->retrieve_details($wherestring);

                                $data = array();
                                $data["pets"] = $petslist['0'];
																$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
                                $data['title'] = "L & R Pets";
                                 
                                 $this->load->view('pets/edit', $data);
                                 
                               

                        }
                        else
                        {
                                $pet_name = $_POST['pet_name'];
                                $species = $_POST['species'];
                                $breed = $_POST['breed'];
                                $weight = $_POST['weight'];
                                $coloring = $_POST['coloring'];
                                $knownconditions = $_POST['knownconditions'];
                                $notes = $_POST['notes'];
                                $id = $wherestring;


                                $this->pet->update_pets($pet_name,$species,$breed,$weight,$coloring,$knownconditions, $notes,$id);
                                 $session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
                                 $data['title'] = "L & R Pets";
                                 
                                   $this->load->view('pets/sucess', $data);
                                   

                                
                        }
					}
					else{
					$petslist = $this->pet->retrieve_details($wherestring);

					$data = array();					
					$data["pets"] = $petslist['0'];
					$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;

                                         $data['title'] = "L & R Pets";
					 
                                           $this->load->view('pets/edit', $data);
                                          
					}

		}

}




/* EOF */
